package com.dao.impl;

import com.dao.DBUtils;
import com.dao.UserDao;
import com.model.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hyhWTX
 * @version 1.0
 * @ClassName UserDaoImpl
 * @date 2022年08月11日 20:12
 * @Description: UserDao接口的实现类，继承DBUtils，使用DBUtils中封装的方法简化数据库操作
 */
public class UserDaoImpl extends DBUtils implements UserDao{
    @Override
    public void addUser(User user) {
        String sql = "insert into tbl_user value(default,?,?) ";
        super.excut(sql,user.getUser_name(),user.getUser_password());
    }

    @Override
    public void upDate(User user) {
        String sql = "update tbl_user set user_id = ?,user_name = ? where user_id = ?";
        super.excut(sql,user.getUser_name(),user.getUser_password(),user.getUser_id());
    }

    @Override
    public void delUser(int userId) {
        String sql = "delete from tbl_user where user_id=?";
        super.excut(sql,userId);

    }

    @Override
    public List<User> queryUser() {

        //连接数据库
        try {
            super.getCon();
            String sql = "select * from tbl_user";
            pre = con.prepareStatement(sql);
            res = pre.executeQuery();
            //创建用于存放User对象的List集合
            List<User> users = new ArrayList<User>();

            //遍历结果集res
            while(res.next()){
                //从结果集中获取数据
                int userId = res.getInt(1);
                String username = res.getString(2);
                String password = res.getString(3);
                //将获得的数据封装到User对象的各个属性中
                User user = new User();
                user.setUser_id(userId);
                user.setUser_name(username);
                user.setUser_password(password);

                //将user对象添加到users集合中
                users.add(user);
            }
            return users;//返回集合
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            super.CloseAll();
        }
        return null;
    }

    @Override
    public User queryUserId(int userId) {
        //连接数据库
        try {
            super.getCon();
            String sql = "select * from tbl_user where user_id=?";
            pre = con.prepareStatement(sql);
            //设置？号占位符的值
            pre.setInt(1,userId);
            res = pre.executeQuery();
            //遍历结果集rs
            if(res.next()){

                //将获得的数据封装到User对象的各个属性中
                User user = new User();
                user.setUser_id(res.getInt(1));
                user.setUser_name(res.getString(2));
                user.setUser_password(res.getString(3));
                return user;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            super.CloseAll();
        }
        return null;
    }

    @Override
    public User queryUserForNamePassWord(String username, String password) {

        //连接数据库
        try {
            super.getCon();
            String sql = "select * from tbl_user where user_name=? and user_password=?";
            pre = con.prepareStatement(sql);
            //设置？号占位符的值
            pre.setString(1,username);
            pre.setString(2,password);
            res = pre.executeQuery();
            //遍历结果集rs
            if(res.next()){

                //将获得的数据封装到User对象的各个属性中
                User user = new User();
                user.setUser_id(res.getInt(1));
                user.setUser_name(res.getString(2));
                user.setUser_password(res.getString(3));
                return user;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            super.CloseAll();
        }
        return null;
    }
    }

